package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dto.ApiResponse;
import com.example.dto.QuestionRequest;
import com.example.entity.Question;
import com.example.service.QuestionService;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    
    /**
     * 添加试题（仅教师和管理员）
     */
    @PostMapping
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<?> addQuestion(@Valid @RequestBody QuestionRequest questionRequest) {
        return questionService.addQuestion(questionRequest);
    }
    
    /**
     * 更新试题（仅教师和管理员）
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<?> updateQuestion(@PathVariable Long id, @Valid @RequestBody QuestionRequest questionRequest) {
        return questionService.updateQuestion(id, questionRequest);
    }
    
    /**
     * 删除试题（仅教师和管理员）
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<?> deleteQuestion(@PathVariable Long id) {
        return questionService.deleteQuestion(id);
    }
    
    /**
     * 获取试题详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('USER') or hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Question> getQuestionById(@PathVariable Long id) {
        Question question = questionService.getQuestionById(id);
        if (question != null) {
            return ApiResponse.success("试题获取成功", question);
        } else {
            return ApiResponse.error("试题不存在");
        }
    }
    
    /**
     * 获取所有试题（仅教师和管理员）
     */
    @GetMapping
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<List<Question>> getAllQuestions() {
        List<Question> questions = questionService.getAllQuestions();
        return ApiResponse.success("试题获取成功", questions);
    }
    
    /**
     * 分页查询试题（包括条件筛选）
     */
    @GetMapping("/page")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<IPage<Question>> getQuestionsByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) String knowledgePoint) {
        IPage<Question> result = questionService.getQuestionsByPage(page, size, type, difficulty, knowledgePoint);
        return ApiResponse.success("试题获取成功", result);
    }
    
    /**
     * 批量导入试题（仅教师和管理员）
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<?> importQuestions(@Valid @RequestBody List<QuestionRequest> questions) {
        return questionService.importQuestions(questions);
    }
} 